<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishopwater_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['price']=$good['price'];
		$v['grouptitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$v['groupid']),'title');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'桶水管理'),
			array('op'=>'edit','title'=>'桶水添加'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：需本场景下购买本分类商品，才会判断缴纳押金！','list'=>array(
			//array('name'=>'title','title'=>'水桶名称','type'=>'string'),
			//array('name'=>'image','title'=>'桶水图片','type'=>'image'),
			//array('name'=>'price','title'=>'水桶价格','type'=>'string'),
			array('name'=>'grouptitle','title'=>'绑定商品分类','type'=>'string'),	
			array('name'=>'ya_fee','title'=>'押金','type'=>'string'),	
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishopwater_goods',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'会场设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'桶水管理'),
			array('op'=>'edit','title'=>'桶水添加'),
		),
		'edit'=>array(
			array('name'=>'groupid','title'=>'绑定商品分类','type'=>'select','options'=>'groups','help'=>'购买本分类的商品，都必须先交押金！'),
			array('name'=>'ya_fee','title'=>'桶押金','type'=>'unitstring','unit'=>'元'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('onepluginname'=>'jy_weishopwater','uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'mid'=>0));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['groupid']){
		exi('请选择分类','error');
	}
	$upgoods=array();
	$upgoods['goodsid']=$postdata['goodsid'];
	$upgoods['groupid']=intval($postdata['groupid']);
	$upgoods['ya_fee']=$postdata['ya_fee'];
	$upgoods['status']=intval($postdata['status']);
	if($id){
		pdo_update('jy_weishopwater_goods',$upgoods,array('id'=>$id));
	}else{
		$upgoods['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishopwater_goods',$upgoods);
		$id=pdo_insertid();
	}
	exi('设置成功！','','/goods/list/');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishopwater_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishopwater_goods',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>